package com.technology.education.ui.student.mine

import android.os.Bundle
import android.view.ViewGroup
import android.widget.ImageView
import androidx.core.view.updateLayoutParams
import androidx.recyclerview.widget.LinearLayoutManager
import com.angcyo.dsladapter.*
import com.google.android.material.imageview.ShapeableImageView
import com.technology.education.R
import com.technology.education.data.SpKeys
import com.technology.education.data.model.UserInfoModel
import com.technology.education.databinding.FragmentStudentMineBinding
import com.technology.education.dslitem.AppStatusItem
import com.technology.education.dslitem.ItemColumnTitleText
import com.technology.education.dslitem.style.DrawableStyle
import com.technology.education.ext.*
import com.technology.education.utils.rom.HuaweiUtils
import me.hgj.jetpackmvvm.base.fragment.BaseVmDbFragment

class MineFragment : BaseVmDbFragment<MineViewModel, FragmentStudentMineBinding>() {
    /**提供悬停功能*/
    var hoverItemDecoration = HoverItemDecoration()

    /**提供基本的分割线功能*/
    var baseDslItemDecoration = DslItemDecoration()

    var myAdapter: DslAdapter = DslAdapter().apply {
        dslAdapterStatusItem = AppStatusItem()
    }

    override fun initView(savedInstanceState: Bundle?) {
        mDatabind.toolBar.toolbar.title = "我的"
        mDatabind.toolBar.toolbar.navigationIcon = null
        initVertical()
    }

    /**
     * 初始化为垂直列表
     */
    private fun initVertical() {
        mDatabind.rvList.apply {
            addItemDecoration(baseDslItemDecoration)
            hoverItemDecoration.attachToRecyclerView(this)
            setHasFixedSize(true)
            layoutManager = LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)
            adapter = myAdapter
            setItemViewCacheSize(20)
        }
    }

    override fun lazyLoadData() {
        if (!isLogin()){
            setUI(null)
        }
    }

    override fun createObserver() {
        getAppViewModel().userInfo.observe(this) {
            setUI(it)
        }
        observe(SpKeys.UPDATE_USER_AVATAR, String::class.java) {
            myAdapter.render {
                updateItem("info")
            }
        }
    }

    override fun showLoading(message: String) {
    }

    override fun dismissLoading() {
    }

    private fun setUI(userInfoModel: UserInfoModel?) {
        myAdapter.render {
            clearAllItems()
            dslItem(R.layout.item_mine_info) {
                itemTag = "info"
                itemBindOverride = { itemHolder, _, _, _ ->
                    itemHolder.tv(R.id.tv_name)?.apply {
                        text = userInfoModel?.name?:"请登录"
                    }
                    itemHolder.tv(R.id.tv_phone)?.text = userInfoModel?.mobile
                    itemHolder.v<ImageView>(R.id.iv_header)?.apply {
                        header(userInfoModel?.avatar, this)
                    }
                    itemHolder.clickItem {
                        if (checkLogin()) {
                            navigation(it, R.id.action_mine_to_manageAccount)
                        }
                    }
                }
            }

            dslItem(R.layout.item_mine_time_remain) {
                itemTag = "time"
                itemBindOverride = { itemHolder, _, _, _ ->
                    itemHolder.tv(R.id.tv_time_remain)?.text =
                        userInfoModel?.durationStr
                    itemHolder.click(R.id.tv_time_record) {
                        if (checkLogin()) {
                            navigation(it, R.id.action_mine_to_scoreRecordFragment)
                        }
                    }
                    itemHolder.click(R.id.tv_pay) {
                        if (checkLogin()) {
                            navigation(it, R.id.action_mine_to_depositFragment)
                        }
                    }
                }
            }

            dslItem(R.layout.item_student_mine_populaize) {
                itemBindOverride = { itemHolder, _, _, _ ->
                    itemTopInsert = 10 * dpi
                    itemBottomInsert = 10 * dpi
                    itemHolder.clickItem {
                        if (checkLogin()) {
                            navigation(it, R.id.action_main_fragment_to_popularizeFragment)
                        }
                    }
                }
            }

            this + ItemColumnTitleText(
                "辅导记录",
                titleDrawable = DrawableStyle(leftDrawable = R.mipmap.icon_mine_note),
                contentDrawable = DrawableStyle(rightDrawable = R.drawable.ic__arrow_right_gray_24)
            ).apply {
                style.topLine = 0
                style.lineColor = R.color.gray_light
                itemClick = {
                    if (checkLogin()) {
                        navigation(it, R.id.action_mine_to_studyRecordFragment)
                    }
                }
            } + ItemColumnTitleText(
                "课程收藏",
                titleDrawable = DrawableStyle(leftDrawable = R.mipmap.icon_mine_collect),
                contentDrawable = DrawableStyle(rightDrawable = R.drawable.ic__arrow_right_gray_24)
            ).apply {
                itemClick = {
                    if (checkLogin()) {
                        navigation(it, R.id.action_main_fragment_to_collectionFragment)
                    }
                }
            }
//            + ItemColumnTitleText(
//                "我的评价",
//                titleDrawable = DrawableStyle(leftDrawable = R.drawable.icon_home),
//                contentDrawable = DrawableStyle(rightDrawable = R.drawable.ic__arrow_right_gray_24)
//            ).apply {
//                itemClick = {
//                    navigation(it, R.id.action_mine_to_setting)
//                }
//            }
            this + ItemColumnTitleText(
                "教师收藏",
                titleDrawable = DrawableStyle(leftDrawable = R.mipmap.icon_mine_work),
                contentDrawable = DrawableStyle(rightDrawable = R.drawable.ic__arrow_right_gray_24)
            ).apply {
                itemClick = {
                    HuaweiUtils.applyPermission(requireContext())
//
//                    if (checkLogin()) {
//                        navigation(it, R.id.action_main_fragment_to_followFragment)
//                    }
                }
            } + ItemColumnTitleText(
                "黑名单管理",
                titleDrawable = DrawableStyle(leftDrawable = R.mipmap.icon_mine_card),
                contentDrawable = DrawableStyle(rightDrawable = R.drawable.ic__arrow_right_gray_24)
            ).apply {
                itemClick = {
                    if (checkLogin()) {
                        navigation(it, R.id.action_main_fragment_to_blackListFragment)
                    }
                }
            }
//            dslItem(R.layout.item_single_image) {
//                itemBindOverride = { itemHolder, _, _, _ ->
//                    itemLeftInsert=10*dpi
//                    itemRightInsert=10*dpi
//                    itemTopInsert=10*dpi
//                    itemBottomInsert=10*dpi
//                    itemHolder.clickItem {
//                        navigation(it, R.id.action_mine_to_popularize)
//                        itemHolder.v<ShapeableImageView>(R.id.iv_image)?.apply {
//                            setBackgroundResource(R.mipmap.img_student_mine_advise)
//                            scaleType=ImageView.ScaleType.CENTER_INSIDE
//                            updateLayoutParams {
//                                this.width = ViewGroup.LayoutParams.MATCH_PARENT
//                            }
//                        }
//                    }
//                }
//            }
            this + ItemColumnTitleText(
                "设置",
                titleDrawable = DrawableStyle(leftDrawable = R.mipmap.setting),
                contentDrawable = DrawableStyle(rightDrawable = R.drawable.ic__arrow_right_gray_24)
            ).apply {
                itemClick = {
                    if (checkLogin()) {
                        navigation(it, R.id.action_main_fragment_to_settingFragment)
                    }
                }
            } + ItemColumnTitleText(
                "联系客服",
                titleDrawable = DrawableStyle(leftDrawable = R.mipmap.icon_heart),
                contentDrawable = DrawableStyle(rightDrawable = R.drawable.ic__arrow_right_gray_24)
            ).apply {
                itemClick = {
                    callPhoneDial(getAppViewModel().userInfo.value?.servicePhone)
                }
            } + ItemColumnTitleText(
                "意见反馈",
                titleDrawable = DrawableStyle(leftDrawable = R.mipmap.icon_mine_advice),
                contentDrawable = DrawableStyle(rightDrawable = R.drawable.ic__arrow_right_gray_24)
            ).apply {
                itemClick = {
                    if (checkLogin()) {
                        navigation(it, R.id.action_main_fragment_to_feedBackFragment)
                    }
                }
            }
        }
    }

}